﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common.Helper
{
    /// <summary>
    /// 验证身份证号码类
    /// </summary>
    public class SfzHelper
    {
        /// <summary>  
        /// 验证身份证合理性  
        /// </summary>  
        /// <param name="idNumber">身份证号码</param>  
        /// <returns>验证通过返回true</returns>  
        public bool CheckIDCard(string idNumber)
        {
            if (idNumber.Length == 18)
            {
                bool check = CheckIDCard18(idNumber);
                return check;
            }
            else if (idNumber.Length == 15)
            {
                bool check = CheckIDCard15(idNumber);
                return check;
            }
            else
            {
                return false;
            }
        }


        /// <summary>  
        /// 18位身份证号码验证  
        /// </summary>  
        private bool CheckIDCard18(string idNumber)
        {
            long n = 0;
            if (long.TryParse(idNumber.Remove(17), out n) == false
                || n < Math.Pow(10, 16) || long.TryParse(idNumber.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证  
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(idNumber.Remove(2)) == -1)
            {
                return false;//省份验证  
            }
            string birth = idNumber.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证  
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = idNumber.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != idNumber.Substring(17, 1).ToLower())
            {
                return false;//校验码验证  
            }
            return true;//符合GB11643-1999标准  
        }


        /// <summary>  
        /// 16位身份证号码验证  
        /// </summary>  
        private bool CheckIDCard15(string idNumber)
        {
            long n = 0;
            if (long.TryParse(idNumber, out n) == false || n < Math.Pow(10, 14))
            {
                return false;//数字验证  
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(idNumber.Remove(2)) == -1)
            {
                return false;//省份验证  
            }
            string birth = idNumber.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证  
            }
            return true;
        }


        /// <summary>
        /// 少数民族怎么办？
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static SplitName GetSplitName(string fullName)
        {
            //保存我国现存的81个复姓（如果还有人是其他复姓，我估计这人是刚出土的）
            String[] RepeatedFamilyName = new String[]{"欧阳","太史",
            "端木","上官","司马","东方","独孤","南宫","万俟","闻人","夏侯",
            "诸葛","尉迟","公羊","赫连","澹台","皇甫","宗政","濮阳","公冶",
            "太叔","申屠","公孙","慕容","仲孙","钟离","长孙","宇文","司徒",
            "鲜于","司空","闾丘","子车","亓官","司寇","巫马","公西","颛孙",
            "壤驷","公良","漆雕","乐正","宰父","谷梁","拓跋","夹谷","轩辕",
            "令狐","段干","百里","呼延","东郭","南门","羊舌","微生","公户",
            "公玉","公仪","梁丘","公仲","公上","公门","公山","公坚","左丘",
            "公伯","西门","公祖","第五","公乘","贯丘","公皙","南荣","东里",
            "东宫","仲长","子书","子桑","即墨","达奚","褚师"};

            try
            {
                if (fullName.Length > 2)
                {
                    var preTwoWords = fullName.Substring(0, 2);//取命名的前两个字,看是否在复姓库中
                    if (RepeatedFamilyName.ToList().Exists(m => m == preTwoWords))
                    {
                        return new SplitName
                        {
                            LastName = preTwoWords,
                            FirstName = fullName.Substring(2)
                        };
                    }
                    return new SplitName
                    {
                        LastName = fullName.Substring(0, 1),
                        FirstName = fullName.Substring(1)
                    };
                }
                if (fullName.Length == 2)
                {
                    return new SplitName
                    {
                        LastName = fullName.Substring(0, 1),
                        FirstName = fullName.Substring(1)
                    };
                }
                return null;
            }
            catch
            {

                return null;
            }
        }

        public class SplitName
        {
            /// <summary>
            /// 名
            /// </summary>
            public string FirstName { get; set; }
            /// <summary>
            /// 姓
            /// </summary>
            public string LastName { get; set; }
        }

    }
}
